package com.zhou.review3.controller;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhou.review3.entity.SysOrderDetail;
import com.zhou.review3.serviceImpl.SysOrderDetailServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/SysOrderDetail")
public class SysOrderDetailController {
    @Autowired
    private SysOrderDetailServiceImpl sysOrderDetailServiceImpl ;

    private final Log log= LogFactory.getLog(SysOrderDetailController.class);
    @RequestMapping("list.do")
    public String list(Model model, @RequestParam(defaultValue = "1")int pageNum, @RequestParam(defaultValue = "5")int pageSize){
        log.info("this is my info log");
        log.error("this is my error log");
        PageHelper.startPage(pageNum,pageSize);
        //获取数据库里student表的所有数据
        List<SysOrderDetail> users=sysOrderDetailServiceImpl.findAllSysOrderDetail();

        PageInfo pageInfo=new PageInfo(users,pageSize);
        model.addAttribute("pageInfo",pageInfo);
//         model.addAttribute("users",users);
        return "listSysOrderDetails";
    }


    //查看内容
//    @RequestMapping("list.do")
//    public String list(Model model){
//        List<SysOrderDetail> sysOrderDetails=sysOrderDetailServiceImpl.findAllSysOrderDetail();
//        model.addAttribute("sysOrderDetails",sysOrderDetails);
//        return "listSysOrderDetails";
//    }

    //删除
    @RequestMapping("delete.do")
    public String delete(int id,Model model){
        sysOrderDetailServiceImpl.removeSysOrderDetail(id);
        return list(model,1,5);
    }

    //toAdd.do,准备增加
    @RequestMapping("toAdd.do")
    public String toAdd(Model model){
        List<SysOrderDetail> sysOrderDetails=sysOrderDetailServiceImpl.findAllSysOrderDetail();
        model.addAttribute("sysOrderDetails",sysOrderDetails);
        return "addSysOrderDetail";
    }
    //增加
    @RequestMapping("add.do")
    public String add(SysOrderDetail sysOrderDetail,Model model){
        sysOrderDetailServiceImpl.addSysOrderDetail(sysOrderDetail);
        return list(model,1,5);
    }

    //更新
    @RequestMapping("toUpdate.do")
    public String toUpdate(int id,Model model){
        SysOrderDetail oldSysOrderDetail=sysOrderDetailServiceImpl.findSysOrderDetail(id);
        model.addAttribute("oldSysOrderDetail",oldSysOrderDetail);
        return "updateSysOrderDetail";
    }
    //update,更新数据到数据库，回到listStudents.html
    @RequestMapping("update.do")
    public String update(SysOrderDetail newSysOrderDetail,Model model){
        sysOrderDetailServiceImpl.modifySysOrderDetail(newSysOrderDetail);
        return list(model,1,5);
    }
}

